clear;
[device, props] = ziCreateAPISession('dev3546', 6);

% Subscribe to the scope's data
ziDAQ('subscribe', ['/' device '/scopes/0/wave']);

% Poll data
poll_length = 0.5; % [s]
poll_timeout = 500; % [ms]
data_trig = ziDAQ('poll', poll_length, poll_timeout);

% Unsubscribe from any streaming data
ziDAQ('unsubscribe', '*');

% Plot the scope data with triggering enabled
scope_in_c='0';
if ziCheckPathInData(data_trig, ['/' device '/scopes/0/wave']);
figure(2); clf;
grid on; box on; hold on;
title('\bf Scope shots (triggering enabled)');
xlabel('t (us)');
ylabel('Amplitude (V)');
scope_shots = data_trig.(device).scopes(1).wave;
fprintf('Number of scope shots with triggering enabled: %d\n', length(scope_shots));
plot_scope_shots(scope_shots, str2num(scope_in_c));
else
scope_shots = [];
end

function plot_scope_shots(scope_shots, scope_in_c)
  num_shots = length(scope_shots);
  c = hsv(num_shots);
  for ii=1:num_shots
    totalsamples = double(scope_shots(ii).totalsamples);
    t = linspace(0, scope_shots(ii).dt*totalsamples, totalsamples);
    % Scale the scope data using ``channelscaling'' field of the scope data
    % structure to obtain physical values for the recorded scope data.
    % Note: ``channeloffset'' will always be 0 unless
    % channels/0/{limitlower,limitupper} have been changed - these are
    % unavailable without the DIG Option.
    wave = scope_shots(ii).channeloffset(scope_in_c+1) + scope_shots(ii).channelscaling(scope_in_c+1)*double(scope_shots(ii).wave(scope_in_c+1, :));
    if ~scope_shots(ii).flags && (length(wave) == totalsamples)
      plot(1e6*t, wave, 'color', c(ii, :));
    end
  end
end